package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.LeaveRequestDTO;
import com.tianya.domain.entity.Clazz;
import com.tianya.domain.entity.Leave;
import com.tianya.domain.entity.User;
import com.tianya.domain.vo.LeaveVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.service.ClazzService;
import com.tianya.service.LeaveService;
import com.tianya.mapper.LeaveMapper;
import com.tianya.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【leave(请假表)】的数据库操作Service实现
* @createDate 2025-05-24 18:59:43
*/
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave>
    implements LeaveService{

    @Resource
    private UserService  userService;

    @Resource
    private ClazzService clazzService;
    @Override
    public void createLeave(LeaveRequestDTO leaveRequestDTO) {
        Leave leave = new Leave();
        BeanUtil.copyProperties(leaveRequestDTO, leave);
        save(leave);
    }

    /**
     * 分页查询请假记录
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param leaveRequestDTO 请假记录
     * @return 分页结果
     */
    public PageResultVO<LeaveVO> pageLeave(Integer pageNum, Integer pageSize, LeaveRequestDTO leaveRequestDTO) {
        Page<Leave> page = Page.of(pageNum, pageSize);
        QueryWrapper<Leave> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(leaveRequestDTO.getStartTime()!=null,Leave::getStartTime, leaveRequestDTO.getStartTime())
                .eq(leaveRequestDTO.getEndTime()!=null,Leave::getEndTime, leaveRequestDTO.getEndTime())
                .eq(leaveRequestDTO.getStatus()!=null,Leave::getStatus, leaveRequestDTO.getStatus())
                .eq(leaveRequestDTO.getUserType()!= null, Leave::getUserType, leaveRequestDTO.getUserType());
        Page<Leave> leavePage = page(page, queryWrapper);
        List<LeaveVO> leaveVOS = new ArrayList<>();
        for (Leave leave : leavePage.getRecords()) {
            Long userId = leave.getUserId();
            Long approverId = leave.getApproverId();
            Long clazzId = leave.getClazzId();
            Clazz clazz = clazzService.getById(clazzId);

            User leaveUser = userService.getById(userId);
            User approverUser = userService.getById(approverId);

            LeaveVO leaveVO = new LeaveVO();
            BeanUtil.copyProperties(leave, leaveVO);
            if (leave.getUserType()==1) {
                leaveVO.setClazzName(clazz!= null ? clazz.getName() : "unKnown");
            }
            leaveVO.setName(leaveUser!= null ? leaveUser.getName() : "unKnown");
            leaveVO.setApproverName(approverUser!= null ? approverUser.getName() : "unKnown");
            leaveVOS.add(leaveVO);
        }
        PageResultVO<LeaveVO> pageVO = new PageResultVO<>();
        pageVO.setTotal(leavePage.getTotal());
        pageVO.setRecords(leaveVOS);
        return pageVO;
    }
}




